const fs = require("fs")
const [computerNum, graphNum, ...graphs] = fs
.readFileSync("./testFile")
.toString()
.trim()
.split("\n")
const graphMap = graphs.reduce((obj, graph) => {
const graphArr = graph.split(" ")
if (Array.isArray(obj[graphArr[0]])) {
obj[graphArr[0]].push(graphArr[1])
} else {
obj[graphArr[0]] = [graphArr[1]]
}
if (Array.isArray(obj[graphArr[1]])) {
obj[graphArr[1]].push(graphArr[0])
} else {
obj[graphArr[1]] = [graphArr[0]]
}
return obj
}, {})
const detechVirusGraph = (key, countSet) => {
if (!Array.isArray(graphMap[key])) {
return countSet
}
return graphMap[key].reduce((cntSet, val) => {
if (cntSet.has(val) || val == 1) {
return cntSet
}
cntSet.add(val)
return detechVirusGraph(val, cntSet)
}, countSet)
}
console.log(detechVirusGraph(1, new Set([])).size)